package com.xiangyi.teachingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyi.teachingsystem.dao.TaskMapper;
import com.xiangyi.teachingsystem.dao.UserGroupMapper;
import com.xiangyi.teachingsystem.entity.po.Task;
import com.xiangyi.teachingsystem.entity.po.UserGroup;
import com.xiangyi.teachingsystem.service.TaskService;
import com.xiangyi.teachingsystem.service.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    TaskMapper taskMapper;
    @Autowired
    UserGroupService userGroupService;

    @Override
    public List<Task> getTaskByCourseId(Integer courseId) {
        return taskMapper.getTaskByCourseId(courseId);
    }

    @Override
    @Transactional
    public void delTask(Integer taskId) {
        List<UserGroup> userGroups = userGroupService.getBaseMapper().selectList(new QueryWrapper<UserGroup>().eq("task_id", taskId));
        for (UserGroup userGroup : userGroups) {
            userGroupService.delGroup(userGroup.getId());
        }
        taskMapper.deleteById(taskId);
    }
}
